package com.kmdd.xtds.util

import android.annotation.SuppressLint
import android.text.TextUtils
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Date

object TimeUtils {
    private val SDF_THREAD_LOCAL: ThreadLocal<Map<String?, SimpleDateFormat?>?> =
        object : ThreadLocal<Map<String?, SimpleDateFormat?>?>() {
            override fun initialValue(): Map<String?, SimpleDateFormat?> {
                return HashMap()
            }
        }
    /**
     * 将毫秒转为日期格式的字符串
     * @param currentTimeMillis 毫秒
     * @param string 日期格式 如 yyyy-MM-dd HH:mm:Ss
     */
    fun millisToString(currentTimeMillis: Long, string: String): String {
        return SimpleDateFormat(string).format(currentTimeMillis);
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss 格式的日期转为毫秒
     * @param string 日期
     */
    fun stringToMillis(string: String): Long {
        if (TextUtils.isEmpty(string)) return 0L
        val date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string)
        return date.time
    }

    /**
     * Formatted time string to the milliseconds.
     *
     * @param time    The formatted time string.
     * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
     * @return the milliseconds
     */
    fun string2Millis(time: String?, pattern: String): Long {
        return string2Millis(time, getSafeDateFormat(pattern))
    }

    @SuppressLint("SimpleDateFormat")
    fun getSafeDateFormat(pattern: String): SimpleDateFormat {
        val sdfMap: MutableMap<String?, SimpleDateFormat?>? = SDF_THREAD_LOCAL.get()?.toMutableMap()
        var simpleDateFormat = sdfMap?.get(pattern)
        if (simpleDateFormat == null) {
            simpleDateFormat = SimpleDateFormat(pattern)
            sdfMap?.set(pattern, simpleDateFormat)
        }
        return simpleDateFormat
    }
    fun string2Millis(time: String?, format: DateFormat): Long {
        try {
            return format.parse(time).time
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return -1
    }
    /**
     * 将yyyy-MM-dd HH:mm:ss 格式的日期转yyyy-MM-dd
     * @param string 日期
     */
    fun stringToYYYY_MM_DD(string: String): String {
        if (TextUtils.isEmpty(string)) return ""
        val date = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string)
        return SimpleDateFormat("yyyy-MM-dd").format(date.time);
    }

    /**
     * 将yyyy-MM-dd HH:mm:ss 格式的日期转为毫秒
     * @param string 日期
     */
    fun stringToMillisByYYYY_MM_DD(string: String): Long {
        if (TextUtils.isEmpty(string)) return 0L
        val date = SimpleDateFormat("yyyy-MM-dd").parse(string)
        return date.time
    }

    /**
     * Date to the formatted time string.
     *
     * @param date    The date.
     * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
     * @return the formatted time string
     */
    fun date2String(date: Date?, pattern: String): String {
        return getSafeDateFormat(pattern).format(date)
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis  The milliseconds.
     * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm
     * @return the formatted time string
     */
    fun millis2String(millis: Long, pattern: String): String? {
        return millis2String(
            millis,
            getSafeDateFormat(pattern)
        )
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis The milliseconds.
     * @param format The format.
     * @return the formatted time string
     */
    fun millis2String(millis: Long, format: DateFormat): String? {
        return format.format(Date(millis))
    }

}